<template>
  <basic-container>
    <avue-crud
        v-if="tableOption"
        v-bind="bindVal"
        v-on="onEvent"
        v-model:page="page"
        v-model="startForm">
      <template #menu="{row,size}">
        <el-button icon="el-icon-download" text type="primary"
                   :size="size"
                   @click="downloadOss(row)">
          下载
        </el-button>


        <el-button icon="el-icon-search" text type="primary"
                   v-if="['.docx','.doc','.xls','.xlsx','.ppt','.pptx','.pdf'].includes(row.ossType.toLowerCase())"
                   :size="size"
                   @click="openDocument(row)">
          打开文档
        </el-button>
      </template>
    </avue-crud>
    <oss-only-office-dialog ref="ossOnlyOfficeDialog" :edit="false"></oss-only-office-dialog>
    <upload-oss-dialog ref="uploadOssDialog" @submit="refreshChange"></upload-oss-dialog>
  </basic-container>
</template>

<script>

import {createDynamicMixin} from "@/mixins/startCrud.js"
import OssOnlyOfficeDialog from "components/start-components/start-office/startOfficeDialog.vue";
import UploadOssDialog from "@/views/start/oss/uploadOssDialog.vue";

import {createDownloadFile, downloadOssByUrl} from "utils/util";
import {securityOssUrl} from "@/config/env";
import useUserStore from "@/stores/useUserStore";
import {getResourceByOssId} from "@/api/start/ossApi";

const option = {}
option.name = "start/oss"

export default {
  computed: {},
  components: {UploadOssDialog, OssOnlyOfficeDialog},
  mixins: [createDynamicMixin(option)],
  data() {
    return {
      userStore: useUserStore(),
    }
  },
  methods: {
    beforeLoadDataSource(search) {
      search.start_order_createTime = "desc"
    },
    beforeRenderDataSource(dataSource) {
      for (const item of dataSource) {
        if (!item.ossAccessType || item.ossAccessType.toLowerCase() !== 'private') {
          continue
        }
        item.ossUrl = securityOssUrl + "/" + item.id + "/" + this.userStore.token;
      }
    },
    openDocument(row) {
      this.$refs.ossOnlyOfficeDialog.show(row.id)
    },
    downloadOss(row) {
      if (!row.ossAccessType || row.ossAccessType.toLowerCase() !== 'private') {
        downloadOssByUrl(row.ossUrl, row.ossName)
        return
      }
      getResourceByOssId(row.id).then(res => {
        createDownloadFile(res)
      })

    },
    beforeOpenAddDialog() {
      this.$refs.uploadOssDialog.show();
    },
  }
}
</script>

<style scoped>

</style>
